package curator.demo;

import com.qs.zk.utils.CuratorClientUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 测试：使用zookeeper的临时顺序节点特性，生成全局id
 *
 * @author qinyupeng
 * @since 2019年04月02日13:46:48
 */
public class GlobalUUID {

    public static void main(String[] args) throws Exception {
        CuratorFramework curatorFramework = CuratorClientUtils.getInstance();
        ExecutorService executorService = Executors.newFixedThreadPool(1000);
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100000; i++) {
                    String path = null;
                    try {
                        path = curatorFramework.create()
                                .creatingParentsIfNeeded()
                                .withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
                                .forPath("/sequence/seq", "测试序列".getBytes());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    System.out.println(path);
                }
            }
        });

        executorService.shutdown();
    }
}
